
package com.fwzc.giuseppe.activity;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.Api.Api;
import com.fwzc.giuseppe.adapter.UniversalAdapter;
import com.fwzc.giuseppe.base.BaseActivity;
import com.fwzc.giuseppe.entity.School;
import com.fwzc.giuseppe.util.ViewHolder;
import com.fwzc.giuseppe.util.isMobileUtil;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

/**
 * 
 * 类名称：RegisterActivity 类描述： 创建人：何琳 创建时间：2015年8月13日 下午12:13:28
 * 
 * @version
 * 
 */
public class RegisterActivity extends BaseActivity implements OnClickListener {

	private TextView regiser;
	private EditText username, code, password,studentId;// 用户名,验证码,密码,学校,学号
	private TextView school;
	private Button getCode;// 获取验证码
	private HttpUtils httpUtil;
	private ProgressDialog load;
	private RelativeLayout choice,schoolView;
	private Dialog schoolDialog;
	private ListView ListView_school;
	private List<School> schoolList = new ArrayList<School>();
	private UniversalAdapter<School> schoolAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_register);

		initBar();
		initView();
		initData();
		initClick();
	}

	private void initView() {
		httpUtil = new HttpUtils();
		load = new ProgressDialog(this);
		load.setMessage(getResources().getString(R.string.deal));
		load.setCancelable(false);
		
		schoolView = (RelativeLayout) LayoutInflater.from(this).inflate(
				R.layout.school_dialog, null);
		ListView_school = (ListView) schoolView.findViewById(R.id.datelist);
		schoolDialog = new Dialog(this);
		schoolDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		schoolDialog.setContentView(schoolView);
		regiser = (TextView) findViewById(R.id.registerBtn);
		username = (EditText) findViewById(R.id.reg_username);
		code = (EditText) findViewById(R.id.reg_code);
		password = (EditText) findViewById(R.id.reg_password);
		school = (TextView) findViewById(R.id.reg_school);
		studentId = (EditText) findViewById(R.id.reg_studentid);

		getCode = (Button) findViewById(R.id.getcode_btn);
		
		schoolAdapter = new UniversalAdapter<School>(this, schoolList,
				R.layout.text_item) {

			@Override
			public void convert(ViewHolder viewHolder, School school) {
				TextView content = (TextView) viewHolder.getView(R.id.content);
				content.setText(school.name);
			}
		};
		ListView_school.setAdapter(schoolAdapter);
	}

	private void initData() {
		actionbar_side_name.setText("注册");
		iv_actionbar_side_left.setVisibility(View.VISIBLE);
		regiser.setOnClickListener(this);
		getCode.setOnClickListener(this);
		iv_actionbar_side_left.setOnClickListener(this);
		
		choice = (RelativeLayout)findViewById(R.id.choice);
	}

	private void initClick(){
		choice.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (schoolList.size() == 0) {
					getAllSchool();
				} else {
					schoolDialog.show();
				}
			}
		});
		
		ListView_school.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				schoolDialog.dismiss();
				school.setText(schoolList.get(position).id+"");
			}
		});

	}
	@Override
	public void onClick(View v) {
		if (v == regiser) {// 注册
			if ("".equals(username.getText().toString())) {
				showToast("请输入手机号!");
				return;
			} else if ("".equals(code.getText().toString())) {
				showToast("请输入验证码!");
				return;
			} else if ("".equals(password.getText().toString())) {
				showToast("请设置密码!");
				return;
			} else {
				sendRegisterRequest();
			}

		} else if (v == getCode) {// 获取验证码
			if (!isMobileUtil.isMobileNumber(username.getText().toString())) {
				showToast("请输入正确手机号！");
				return;
			} else {
				sendgetCodeRequest();
			}
		} else if (v == iv_actionbar_side_left) {
			finish();
		}

	}

	/**
	 * 发送注册请求
	 */
	private void sendRegisterRequest() {
		RequestParams params = new RequestParams("UTF-8");
		params.addBodyParameter("userName", username.getText().toString());
		params.addBodyParameter("authenticationCode", code.getText().toString());
		params.addBodyParameter("password", password.getText().toString());
		params.addBodyParameter("school", school.getText().toString());
		params.addBodyParameter("studentNumber", studentId.getText().toString());
		// 发送json类型的数据
		// params.setBodyEntity(new StringEntity(jsonStr,charset));
		httpUtil.send(HttpRequest.HttpMethod.POST, Api.REGISTER, params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						load.show();
					}

					@Override
					public void onFailure(HttpException arg0, String response) {
						load.dismiss();
						showToast("加载数据失败");
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseStr) {
						try {
							JSONObject response = new JSONObject(responseStr.result);
							int resultCode = response.getInt("resultCode");
							if (resultCode == 0) {
								load.dismiss();
								showToast("注册成功");
							}else{
								load.dismiss();
								showToast(response.getString("msg"));
							}
						} catch (JSONException e) {
							load.dismiss();
							showToast("解析出错");
						}

					}
				});
	}

	/**
	 * 获取验证码请求
	 */
	private void sendgetCodeRequest() {
		RequestParams params = new RequestParams("UTF-8");
		params.addBodyParameter("phoneNumber", username.getText().toString());
		// 发送json类型的数据
		// params.setBodyEntity(new StringEntity(jsonStr,charset));
		httpUtil.send(HttpRequest.HttpMethod.POST, Api.GET_CODE, params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						
					}

					@Override
					public void onFailure(HttpException arg0, String response) {
						showToast("加载数据失败");
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseStr) {
						try {
							JSONObject response = new JSONObject(responseStr.result);
							int resultCode = response.getInt("resultCode");
							if (resultCode == 0) {
								showToast("获取验证码成功");
							}
						} catch (JSONException e) {
							showToast("解析出错");
						}

					}
				});
	}
	
	private void getAllSchool() {
		RequestParams params = new RequestParams("UTF-8");
		// 发送json类型的数据
		httpUtil.send(HttpRequest.HttpMethod.POST, Api.ALLSCHOOL, params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						load.show();
					}

					@Override
					public void onFailure(HttpException arg0, String response) {
						load.dismiss();
						showToast("加载数据失败");
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseStr) {
						try {
							JSONObject response = new JSONObject(
									responseStr.result);
							int resultCode = response.getInt("resultCode");
							if (resultCode == 1) {
								JSONArray ja = response
										.getJSONArray("dataList");
								for (int i = 0; i < ja.length(); i++) {
									School school = new School();
									school.id = ja.getJSONObject(i).getString(
											"schoolid");
									school.name = ja.getJSONObject(i)
											.getString("schoolname");
									schoolList.add(school);
								}
								schoolAdapter.notifyDataSetChanged();
								load.dismiss();
								schoolDialog.show();
							} else {
								load.dismiss();
								showToast(response.getString("msg"));
							}
						} catch (JSONException e) {
							load.dismiss();
							showToast("解析出错");
						}

					}
				});
	}

}
